package utils

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"time"
)

func MongoConnectCars() bson.M {
	var result bson.M
	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
	client_option := options.Client().ApplyURI("mongodb://localhost:27017")
	clienta, err := mongo.Connect(ctx, client_option)

	if err != nil {
		fmt.Print("MongoDB连接失败", err.Error())
		return nil
	}
	// 确认连接状态
	err = clienta.Ping(ctx, nil)
	if err != nil {
		fmt.Print("MongoDB连接失败", err.Error())
		return nil
	}
	collection := clienta.Database("big_data").Collection("cars_data")
	err = collection.FindOne(ctx, bson.M{}).Decode(&result)
	if err != nil {
		fmt.Print("查询carsdata失败", err.Error())
	}

	return result
}
